package com.uobgroup.ews.uam.core.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.uobgroup.ews.uam.core.domain.ApplicationConfiguration;

@Repository
public class ApplicationConfigurationDaoImpl extends AbstractJpaDao<ApplicationConfiguration> implements
      ApplicationConfigurationDao {
   
   private static final String MAX_LOGIN_ATTEMPT = "maxLoginAttempt";

   public ApplicationConfigurationDaoImpl() {
      setClazz(ApplicationConfiguration.class);
   }
   
   @Override
   public long count() {
      return entityManager.createQuery("SELECT COUNT(o) FROM ApplicationConfiguration o", Long.class).getSingleResult();
   }

   @Override
   public List<ApplicationConfiguration> findAll(int firstResult, int maxResults) {
      return entityManager.createQuery("SELECT o FROM ApplicationConfiguration o", ApplicationConfiguration.class)
            .setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
   }

   @Override
   public List<ApplicationConfiguration> findUsernameDomain(String applicationConfigKey) {
      return entityManager.createQuery(
            "SELECT o FROM ApplicationConfiguration o Where o.propertyKey='" + applicationConfigKey + "'",
            ApplicationConfiguration.class).getResultList();
	   
   }

   /*
    * Phuong Tran
    */
   @Override
   public Integer getMaxLoginAttempt() {
      Integer max = 0;
      ApplicationConfiguration appConfig = entityManager.createQuery(
            "SELECT o FROM ApplicationConfiguration o Where o.propertyKey='" + MAX_LOGIN_ATTEMPT + "'",
            ApplicationConfiguration.class).getSingleResult();
      max = Integer.parseInt(appConfig.getPropertyValue());
      return max;
   }

}
